#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

void pr_mask(const char *str)
{
    sigset_t sigset;
    int errno_save;

    errno_save = errno; /* we can be called by signal handlers */
    if (sigprocmask(0, NULL, &sigset) < 0)
    {
        perror("sigpromask error");
        exit(0);
    }

    printf("%s", str);

    if (sigismember(&sigset, SIGINT)) printf("SIGINT ");
    if (sigismember(&sigset, SIGUSR1)) printf("SIGUSR1 ");

    printf("\n");
    errno = errno_save;
}


void handler(int sig)
{
    pr_mask("In handler ");
    printf("sleeping.\n");
    pause();
    printf("waking up.\n");

    return;
}

int main(int argc, char const* argv[])
{
    struct sigaction act, oact;

    pr_mask("starting main");

    act.sa_handler = handler;

    sigemptyset(&act.sa_mask);
    sigaddset(&act.sa_mask, SIGUSR1);

    act.sa_flags = SA_NODEFER | SA_RESETHAND;

    if (sigaction(SIGINT, &act, &oact) == -1)
    {
        perror("sigaction error");
        exit(0);
    }

    for (;;)
        pause();

    return 0;
}
